/**
 * 系统主题设置服务
 */

/**
 * 主题配置数据
 */
export const themeOptions = [
  {
    id: 'default',
    name: '默认主题',
    description: '清新简洁的浅色主题，适合日常使用',
    icon: 'el-icon-sunny',
    primaryColor: '#409EFF',
    backgroundColor: '#ffffff',
    textColor: '#333333',
    preview: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)'
  },
  {
    id: 'classic',
    name: '经典主题',
    description: '沉稳大气的深蓝主题，专业感十足',
    icon: 'el-icon-s-operation',
    primaryColor: '#1890ff',
    backgroundColor: '#f0f2f5',
    textColor: '#262626',
    preview: 'linear-gradient(135deg, #1e3c72 0%, #2a5298 100%)'
  },
  {
    id: 'dark',
    name: '暗黑模式',
    description: '深色护眼主题，适合夜间或弱光环境使用',
    icon: 'el-icon-moon',
    primaryColor: '#177ddc',
    backgroundColor: '#141414',
    textColor: '#ffffff',
    preview: 'linear-gradient(135deg, #232526 0%, #414345 100%)'
  },
  {
    id: 'green',
    name: '护眼模式',
    description: '舒适的绿色主题，减少视觉疲劳',
    icon: 'el-icon-view',
    primaryColor: '#52c41a',
    backgroundColor: '#f6ffed',
    textColor: '#389e0d',
    preview: 'linear-gradient(135deg, #11998e 0%, #38ef7d 100%)'
  },
  {
    id: 'contrast',
    name: '高对比度',
    description: '强对比配色，提升内容可读性',
    icon: 'el-icon-s-opportunity',
    primaryColor: '#ff4d4f',
    backgroundColor: '#ffffff',
    textColor: '#000000',
    preview: 'linear-gradient(135deg, #000000 0%, #434343 100%)'
  }
];

/**
 * 字体大小选项
 */
export const fontSizeOptions = [
  { label: '标准', value: 'normal', size: '14px', description: '默认字体大小' },
  { label: '大', value: 'large', size: '16px', description: '适合阅读更多文字' },
  { label: '特大', value: 'xlarge', size: '18px', description: '最适合长时间阅读' }
];

/**
 * 动画效果选项
 */
export const animationOptions = [
  { label: '流畅', value: 'smooth', description: '完整的过渡动画效果' },
  { label: '标准', value: 'normal', description: '适中的动画速度' },
  { label: '关闭', value: 'none', description: '关闭所有动画效果，提升性能' }
];

/**
 * 获取当前主题设置
 */
export function getThemeSettings() {
  return new Promise((resolve) => {
    setTimeout(() => {
      // 模拟从服务器或本地存储获取设置
      const settings = {
        theme: 'default',
        fontSize: 'normal',
        animation: 'normal',
        autoNightMode: false,
        nightModeTime: {
          start: '20:00',
          end: '07:00'
        },
        followSystem: false,
        reducedMotion: false
      };
      resolve(settings);
    }, 300);
  });
}

/**
 * 更新主题设置
 */
export function updateThemeSettings(settings) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      // 模拟保存到服务器
      if (settings && settings.theme) {
        console.log('主题设置已保存:', settings);
        resolve({ success: true, message: '主题设置已保存' });
      } else {
        reject(new Error('保存失败，请重试'));
      }
    }, 500);
  });
}

/**
 * 获取默认主题设置
 */
export function getDefaultThemeSettings() {
  return {
    theme: 'default',
    fontSize: 'normal',
    animation: 'normal',
    autoNightMode: false,
    nightModeTime: {
      start: '20:00',
      end: '07:00'
    },
    followSystem: false,
    reducedMotion: false
  };
}

/**
 * 应用主题到页面
 */
export function applyTheme(themeId) {
  const theme = themeOptions.find(t => t.id === themeId);
  if (!theme) return;

  const root = document.documentElement;
  
  // 设置主题CSS变量
  root.style.setProperty('--theme-primary', theme.primaryColor);
  root.style.setProperty('--theme-bg', theme.backgroundColor);
  root.style.setProperty('--theme-text', theme.textColor);
  
  // 根据主题调整次要颜色
  if (themeId === 'dark') {
    root.style.setProperty('--theme-bg-secondary', '#1f1f1f');
    root.style.setProperty('--theme-border', '#3a3a3a');
    root.style.setProperty('--theme-shadow', 'rgba(255, 255, 255, 0.05)');
    document.body.setAttribute('data-theme', 'dark');
  } else if (themeId === 'green') {
    root.style.setProperty('--theme-bg-secondary', '#f6ffed');
    root.style.setProperty('--theme-border', '#b7eb8f');
    root.style.setProperty('--theme-shadow', 'rgba(82, 196, 26, 0.08)');
    document.body.setAttribute('data-theme', 'green');
  } else if (themeId === 'contrast') {
    root.style.setProperty('--theme-bg-secondary', '#f5f5f5');
    root.style.setProperty('--theme-border', '#000000');
    root.style.setProperty('--theme-shadow', 'rgba(0, 0, 0, 0.2)');
    document.body.setAttribute('data-theme', 'contrast');
  } else if (themeId === 'classic') {
    root.style.setProperty('--theme-bg-secondary', '#f0f2f5');
    root.style.setProperty('--theme-border', '#d9d9d9');
    root.style.setProperty('--theme-shadow', 'rgba(0, 0, 0, 0.08)');
    document.body.setAttribute('data-theme', 'classic');
  } else {
    root.style.setProperty('--theme-bg-secondary', '#f5f5f5');
    root.style.setProperty('--theme-border', '#e8e8e8');
    root.style.setProperty('--theme-shadow', 'rgba(0, 0, 0, 0.1)');
    document.body.setAttribute('data-theme', 'default');
  }

  // 更新Element UI组件主题色
  root.style.setProperty('--el-color-primary', theme.primaryColor);
  
  // 存储到本地
  localStorage.setItem('app-theme', themeId);
  
  console.log(`✅ 主题已应用: ${theme.name}`);
}

/**
 * 应用字体大小
 */
export function applyFontSize(fontSize) {
  const option = fontSizeOptions.find(f => f.value === fontSize);
  if (!option) return;

  const root = document.documentElement;
  root.style.setProperty('--font-size-base', option.size);
  
  // 根据基础字体大小调整其他字体大小
  const baseSizeNum = parseInt(option.size);
  root.style.setProperty('--font-size-small', `${baseSizeNum - 2}px`);
  root.style.setProperty('--font-size-large', `${baseSizeNum + 2}px`);
  
  localStorage.setItem('app-font-size', fontSize);
  
  console.log(`✅ 字体大小已应用: ${option.label} (${option.size})`);
}

/**
 * 应用动画设置
 */
export function applyAnimation(animation) {
  const root = document.documentElement;
  const duration = animation === 'none' ? '0s' : animation === 'smooth' ? '0.5s' : '0.3s';
  
  root.style.setProperty('--animation-duration', duration);
  
  // 如果关闭动画，禁用所有过渡效果
  if (animation === 'none') {
    root.style.setProperty('--animation-easing', 'step-end');
  } else {
    root.style.setProperty('--animation-easing', 'ease');
  }
  
  localStorage.setItem('app-animation', animation);
  
  console.log(`✅ 动画效果已应用: ${animation}`);
}

/**
 * 初始化主题 - 从本地存储加载
 */
export function initTheme() {
  const savedTheme = localStorage.getItem('app-theme') || 'default';
  const savedFontSize = localStorage.getItem('app-font-size') || 'normal';
  const savedAnimation = localStorage.getItem('app-animation') || 'normal';
  
  applyTheme(savedTheme);
  applyFontSize(savedFontSize);
  applyAnimation(savedAnimation);
  
  console.log('🎨 主题系统已初始化');
}

